/**
 *	@file	pgtel_key.c
 *	@brief	ポケギア　キー取得
 *	@author	Miyuki Iwasawa
 *	@date	07.10.16
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/wipe.h"
#include "system/buflen.h"
#include "gflib/touchpanel.h"
#include "application/app_tool.h"
#include "application/pokegear.h"
#include "msgdata/msg.naix"

#include "../pgear_sys.h"
#include "../pgear_frm.h"
#include "../pgear_act.h"
#include "pgtel_sys.h"
#include "pgtel_bg.h"
#include "pgtel_sub.h"

#define __PGTEL_KEY_H_GLOBAL
#include "pgtel_key.h"


/*
 *	@brief	通常アプリキー操作
 */
int PGTelKey_ContKey(PGTEL_SYS_WORK* wk)
{
	int ret;

	if(sys.trg & PAD_BUTTON_B && !wk->input_f){	//システムキーモードに戻る
		wk->sys->control = PGCONT_SYS;
		Snd_SePlay(SE_PGEAR_CANCEL);
		CursorMvMan_SetCursorDrawFlag(wk->sys->curMan,PGKM_SYS_PARAM,TRUE);
		CursorMvMan_SetCurrent(wk->sys->curMan,PGKM_SYS_PARAM,PGFrame_GetSCurModePosNo(wk->sys));
		PGTelVList_CursorSet(&wk->vList,0xFF,FALSE);
		return PG_NEXT_NULL;
	}
	ret = PGTelVList_KeyIn(&wk->vList);
	if(ret >= 0){
		//ターゲット指定
		wk->target = wk->vList.tag[ret].number;
		Snd_SePlay(SE_PGEAR_DECIDE);
		PGTelVList_CursorAnimeSet(&wk->vList,FALSE);
		PGTelSub_SelBoxSet(wk,PGTelVList_GetCurrentPos(&wk->vList),SBOX_ID_CALL);
		PGTelSub_ExpWinDraw(wk,MIDX_EXP_CONTROL,TRUE);
		return PG_NEXT_APP_SEQ;
	}
	return PG_NEXT_NULL;
}

/**
 *	@brief	通常アプリタッチ操作
 */
int PGTelKey_ContTouch(PGTEL_SYS_WORK* wk)
{
	int ret = PG_NEXT_NULL;

	if(!wk->input_f){	//スクロール中はシステムボタンはチェックしない
		ret = PGFrame_TpControl(wk->sys);
		if(ret != PG_NEXT_NULL){
			wk->sys->key_mode = APP_KTST_TOUCH;
			return ret;
		}
	}

	ret = PGTelVList_TouchIn(&wk->vList);
	if(ret >= 0){
		if(wk->sys->control == PGCONT_SYS){
			PGTelSub_ToTouchChange(wk);
		}
		if(ret == 0){	//ページスクロール
			return PG_NEXT_NULL;
		}
		ret -= 1;
		wk->target = wk->vList.tag[ret].number;
		Snd_SePlay(SE_PGEAR_DECIDE);
//		PGTelVList_SetCurrentPos(&wk->vList,ret);
		PGTelVList_CursorAnimeSet(&wk->vList,FALSE);
		PGTelSub_SelBoxSet(wk,PGTelVList_GetCurrentPos(&wk->vList),SBOX_ID_CALL);
		PGTelSub_ExpWinDraw(wk,MIDX_EXP_CONTROL,TRUE);

		wk->sys->key_mode = APP_KTST_TOUCH;
		return PG_NEXT_APP_SEQ;
	}
	return PG_NEXT_NULL;
}

/**
 *	@brief	ソートモードキー操作
 */
int PGTelKey_ContSortKey(PGTEL_SYS_WORK* wk)
{
	int ret,touch_f = FALSE;
#if 0
	if(wk->input_f == 0){
		//キーorタッチ操作の切替取得
		if(PGFrame_ChangeTPkey(wk->sys)){
			return 0;
		}
	}
	if(wk->sys->key_mode == APP_KTST_TOUCH){
		ret = PGTelVList_TouchInSort(&wk->vList);
	}else{
		ret = PGTelVList_KeyInSort(&wk->vList);
	}
#else
	ret = PGTelVList_TouchInSort(&wk->vList,&touch_f);
	if(!touch_f){
		if(!wk->input_f){
			PGFrame_ChangeKeyModeCheck(wk->sys);
		}
		ret = PGTelVList_KeyInSort(&wk->vList);
	}else{
		wk->sys->key_mode = APP_KTST_TOUCH;
	}
#endif
	if(ret < 0){
		return 0;
	}
	if(	ret == wk->vList.target ){
		IWASAWA_PRINT(" 挿入キャンセル\n");
		return 1;
	}
	IWASAWA_PRINT(" 入れ替え発生 %d を %d へ\n",wk->vList.target,ret);
	PGTelList_Insert(wk,wk->vList.tag[wk->vList.target].pl,ret);
	wk->vList.target = ret;

	//リスト再生成
	PGTelVList_VListMake(wk);
	PGTelVList_Start(wk,&wk->vList,wk->vList.top,wk->vList.pos);
	return 1;
}